//
//  MyViewController.swift
//  ShiZhe
//
//  Created by 陈卿伟 on 16/8/23.
//  Copyright © 2016年 陈卿伟. All rights reserved.
//

import UIKit

class MyViewController: MainViewController {
    var tableView:MyTableView!
    var firstCell:MyFirstCell!
    var otherCell:MyOtherCell!
    
    
    let avatarImage = 0
    let userName = "创微网络 陈卿伟"
    let rank = "第 32412 人"
    let concernsNum = 37
    let fansNum = 200
    let commentNum = 999
    let leftText = ["我的干货","我的回答","我的提问","我的收藏","我的阅读记录","我的财产","我的三人行地位"]
    let righttext = ["234.4K","No.42412"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "我的"
        self.addTableView()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(true)
        self.navigationController?.navigationBar.hidden = true
        UIApplication.sharedApplication().statusBarStyle = .Default
        self.automaticallyAdjustsScrollViewInsets = false
    }
}

//MARK:--UI设置
extension MyViewController{
    //添加tableView
    func addTableView(){
        tableView = MyTableView(frame: MainBounds, style: .Grouped)
        tableView.delegate = self
        tableView.dataSource = self
        self.view.addSubview(tableView)
    }
    
    //对第一个cell的操作
    func operationToFirstCell(cell:MyFirstCell){
        //给设置按钮添加点击事件
        cell.settingIcon.addTarget(self, action: #selector(self.settingIconClick(_:)), forControlEvents: .TouchUpInside)
        
        //给头像添加图片
        let avatar = cell.avatar
        if avatarImage == 0{
            //没有头像  使用系统内置
            avatar?.image = UIImage(named: "noAvatar")
        }else{
            avatar?.image = UIImage(named: "avatar2")
        }
        //给头像添加点击事件
        let tap = UITapGestureRecognizer(target: self, action: #selector(self.avatarClick(_:)))
        avatar?.addGestureRecognizer(tap)
        
        //给userName赋值
        cell.userName.text = userName
        
        //给rank赋值
        cell.rankLabel.text = rank
        
        //给concernsLabel赋值并添加点击事件
        let concerns = cell.concernsLabel
        concerns?.text = "\(concernsNum)"
        let tapConcerns = UITapGestureRecognizer(target: self, action: #selector(self.concernsClick(_:)))
        concerns?.addGestureRecognizer(tapConcerns)
        
        //给fansLabel赋值并添加点击事件
        let fansLabel = cell.fansLabel
        fansLabel?.text = "\(fansNum)"
        let tapfansLabel = UITapGestureRecognizer(target: self, action: #selector(self.fansLabelClick(_:)))
        fansLabel?.addGestureRecognizer(tapfansLabel)
        
        //给comment赋值并添加点击事件
        let comment = cell.commentLabel
        if commentNum>99{
            comment?.text = "99+"
        }else{
            comment?.text = "\(commentNum)"
        }
        let tapComment = UITapGestureRecognizer(target: self, action: #selector(self.commentLabelClick(_:)))
        comment?.addGestureRecognizer(tapComment)
        
        //判断小圆点是否要出现
        if commentNum == 0{
            cell.smallDot.alpha = 0
        }
    }
}

//MARK:--点击事件
extension MyViewController{
    //设置按钮点击事件
    func settingIconClick(sender:UIButton){
        self.navigationController?.pushViewController(SettingViewController(), animated: true)
    }
    
    //头像点击事件
    func avatarClick(sender:UITapGestureRecognizer){
        print("点击了头像")
    }
    
    //关注点击事件
    func concernsClick(sender:UITapGestureRecognizer){
        print("点击了关注")
    }
    
    //关注点击事件
    func fansLabelClick(sender:UITapGestureRecognizer){
        print("点击了粉丝")
    }
    
    //评论的点击事件
    func commentLabelClick(sender:UITapGestureRecognizer){
        print("点击了评论")
    }
}

//tableView  delegate  datasource
extension MyViewController:UITableViewDelegate,UITableViewDataSource{

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 4
    }
    
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0:
            return 1
        case 1:
            return 3
        case 2:
            return 2
        case 3:
            return 2
        default:
            return 0
        }
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        if (indexPath as NSIndexPath).section == 0{
            firstCell = MyFirstCell(style: .Default, reuseIdentifier: "firstCell")
            self.operationToFirstCell(firstCell)
            tableView.rowHeight = 274*SCALEH
            return firstCell
        }else{
            otherCell = MyOtherCell(style: .Default, reuseIdentifier: "otherCell")
            let leftLabel = otherCell.leftLabel
            let rightLabel = otherCell.rightLabel
            switch (indexPath as NSIndexPath).section{
            case 1:
                leftLabel?.text = leftText[(indexPath as NSIndexPath).row]
            case 2:
                leftLabel?.text = leftText[(indexPath as NSIndexPath).row+3]
            case 3:
                leftLabel?.text = leftText[(indexPath as NSIndexPath).row+5]
                rightLabel?.text = righttext[(indexPath as NSIndexPath).row]
            default:
                break
            }
            tableView.rowHeight = 40*SCALEH
            return otherCell
        }
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        self.tableView.refresh.endRefreshing()
    }
}













